package com.xckj.ibox.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.ibox.R;
import com.xckj.ibox.bean.HomeGoodsBean;
import com.xckj.ibox.iview.IHomeView;
import com.xckj.ibox.utils.LoadImageUtil;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 首页商品
 * @Date: 2023/6/16 18:48
 */
public class HomeGoodsAdapter extends RecyclerView.Adapter {
    private List<HomeGoodsBean.DataBean> homeGoods;
    private OnItemClickListener onItemClickListener;


    @SuppressLint("NotifyDataSetChanged")
    public void setHomeGoodsData(List<HomeGoodsBean.DataBean> homeGoods) {
        this.homeGoods = homeGoods;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_goods, parent, false));
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(view, position);
            }
        });
        HomeGoodsBean.DataBean homeGoodsBean = homeGoods.get(position);
        LoadImageUtil.loadImage(homeGoodsBean.image, itemViewHolder.ivGoodsPicture);
        itemViewHolder.tvGoodsName.setText(homeGoodsBean.name);
        itemViewHolder.tvNumber.setText(String.format("发行%d | 已售%d", homeGoodsBean.stock, homeGoodsBean.sales));
        itemViewHolder.tvGoodsPrice.setText(String.format("￥%s", homeGoodsBean.price));
        switch (homeGoodsBean.status) {
            /*售卖中*/
            case 1:
                itemViewHolder.ivGoodsState.setImageResource(R.mipmap.icon_for_sale);
                itemViewHolder.tvGoodsState.setText("在售中");
                break;
            /*已售罄*/
            case 2:
                itemViewHolder.ivGoodsState.setImageResource(R.mipmap.icon_sell_out);
                itemViewHolder.tvGoodsState.setText("已售罄");
                break;
            /*即将开售*/
            case 3:
                itemViewHolder.ivGoodsState.setImageResource(R.mipmap.icon_coming_soon);
                itemViewHolder.tvGoodsState.setText(String.format("即将发售%s", homeGoodsBean.status_time_text));
                break;
            default:
                break;
        }
        if (0 == homeGoodsBean.is_admin) {
            itemViewHolder.ivOfficialGoods.setVisibility(View.INVISIBLE);
        } else {
            itemViewHolder.ivOfficialGoods.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        return homeGoods == null ? 0 : homeGoods.size();
    }

    private void initView() {

    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ImageView ivGoodsPicture;
        private final ImageView ivGoodsState;
        private final TextView tvGoodsState;
        private final TextView tvGoodsName;
        private final TextView tvNumber;
        private final TextView tvGoodsPrice;
        private final ImageView ivOfficialGoods;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ivGoodsPicture = itemView.findViewById(R.id.iv_goods_picture);
            ivGoodsState = itemView.findViewById(R.id.iv_goods_state);
            tvGoodsState = itemView.findViewById(R.id.tv_goods_state);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvNumber = itemView.findViewById(R.id.tv_number);
            tvGoodsPrice = itemView.findViewById(R.id.tv_goods_price);
            ivOfficialGoods = itemView.findViewById(R.id.iv_official_goods);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item子View的点击事件
         *
         * @param view     当前视图
         * @param position 当前索引
         */
        void onItemClick(View view, int position);
    }
}
